fix: correlate invalid JSON-RPC envelope errors with the original request id#2852
Open
anneheartrecord wants to merge 1 commit into
Conversation
…uest id When an incoming message is valid JSON but fails JSON-RPC envelope validation, the error response previously could not be correlated with the originating request: the stdio server transport dropped the message with no response at all, and the Streamable HTTP transport replied with a null id. Extract the request id best-effort from the raw parsed payload and preserve it in the error response on both transports, falling back to a null id (per the JSON-RPC 2.0 spec) when no valid id can be extracted. The stdio transport now also answers unparseable lines with a Parse error (-32700, null id), and both transports report envelope-invalid messages as Invalid Request (-32600) instead of Invalid params (-32602), matching the JSON-RPC 2.0 error code semantics.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2848
When an incoming message is valid JSON but fails JSON-RPC envelope validation (e.g.
"jsonrpc": "1.0", a missingjsonrpcfield, or a non-stringmethod), the error response could not be correlated with the originating request:"server-error"on v1.x), breaking client-side request/response correlation.This PR extracts the request id best-effort from the raw parsed payload (shared helper
extract_raw_request_id; only spec-valid id types are accepted — strings and non-bool integers) and preserves it in the error response on both transports, falling back to a null id per the JSON-RPC 2.0 spec when no valid id can be extracted. The stdio transport now also answers unparseable lines with-32700 Parse error(null id), matching the HTTP transport's existing parse-error behavior. Envelope-invalid messages are now reported as-32600 Invalid Requestinstead of-32602 Invalid params, matching JSON-RPC 2.0 error code semantics (the issue's three repro cases are envelope violations, not parameter errors).Tests cover all three repro cases from the issue on both transports, plus the null-id fallback (unextractable id and unparseable body). Full suite,
ruff, andpyrightpass.